package dao

import (
	"enotify/pkg/async/conf"
	"github.com/gomodule/redigo/redis"
	"time"
)


type Dao struct {
	c *conf.Config
	redis *redis.Pool
}
func New(c *conf.Config) *Dao {
	return &Dao{
		c:     c,
		redis: newRedis(c.Redis),
	}
}
func newRedis(c *conf.Redis) *redis.Pool {
	return &redis.Pool{
		MaxIdle: c.Idle,
		MaxActive: c.Active,
		IdleTimeout: time.Duration(c.IdleTimeout),
		Dial: func() (redis.Conn, error) {
			conn, err := redis.Dial(c.Network, c.Addr,
			redis.DialConnectTimeout(time.Duration(c.DialTimeout)),
			redis.DialReadTimeout(time.Duration(c.ReadTimeout)),
			redis.DialWriteTimeout(time.Duration(c.WriteTimeout)),
			redis.DialPassword(c.Auth),
				)
			if err != nil {
				return nil, err
			}
			return conn,nil
		},
	}
}
func (d *Dao) Close() error {
	return d.redis.Close()
}
